/**
 * Instances of this class have their counterparts in the boxes on board. 
 * @author mx109
 *
 */
public class Box implements MoveableObject {
	private Position pos;
	private int id=0;
	private static int i=0;
	public Board board;
	
	@SuppressWarnings("unused")
	private Box(){}
	
	/**
	 * Create new box on chosen position in given board. 
	 * @param a  position for new box
	 * @param b  board on which the box will be placed 
	 */
	public Box(Position a, Board b){ 
		board=b;
		setPosition(a); 
		id=i++;}
	
	/** Copy constructor used by Checker class
	 * 
	 *  @author crezax
	 */
	public Box(Box b, Board board){
		pos = new Position(b.pos);
		id = b.id;
		this.board = board;
	}
	
	@Override
	public boolean equals(Object o){
		if (!(o instanceof Box)) return false;
		return id == ((Box) o).id;
	}
	
	@Override
	public Position getPosition() {return pos;}

	@Override
	public void setPosition(Position a) {this.pos=new Position(a.x,a.y);}

	@Override
	public boolean move(Direction dir) {
	//	System.out.println("Jestem"+this.pos+" chcesz mnie ruch "+dir);
		Position p=new Position(this.pos.x, this.pos.y);
		if (dir==Direction.DOWN) p.y++;
		if (dir==Direction.UP) p.y--;
		if (dir==Direction.LEFT) p.x--;
		if (dir==Direction.RIGHT) p.x++;
		if (p.x>=board.rozx || p.x<0 || p.y>=board.rozy || p.y<0)return false;
		Field next=board.check(p);
		if (next.movObj!=null || next.type==Type.WALL) return false;
		board.check(pos).movObj=null;
		board.check(p).movObj=this;
		pos=p;
		
		return true;
	}
	

	protected static void setZero(){i=0;}
	@Override
	public int getId() {return id;}
	

}
